package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.service.UserInfoVisitVOService;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.domain.UserInfoAttention;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.comment.service.IUserInfoAttentionService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/mine")
public class PersonalCenterController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private ITravelStatisVOService travelStatisVOService;
    @Reference
    private IUserInfoAttentionService userInfoAttentionService;
    @Reference
    private UserInfoVisitVOService infoVisitVO;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @RequestMapping("/home")
    @RequireLogin
    public String home(Model model, @UserParam UserInfo userInfo){


        UserInfo user = userInfoService.get(userInfo.getId());

        List<Travel> travels = null;
        travels = travelService.get2TravelByUserId(user.getId());
        model.addAttribute("count",travels.size());
        //从redis中获取数据
        for (Travel travel : travels) {
            TravelStatisVO travelStatisVO = travelStatisVOService.getTravelStatisVO(travel.getId());
            travel.setViewnum(travelStatisVO.getViewnum());
            travel.setSharenum(travelStatisVO.getSharenum());
            travel.setReplynum(travelStatisVO.getReplynum());
            travel.setFavornum(travelStatisVO.getFavornum());
        }
        if (travels.size()>2) {
            travels = travels.subList(0, 2);
        }
        //共享用户信息
        model.addAttribute("userInfo",user);
        //共享游记信息
        model.addAttribute("travels",travels);

        List<UserInfoAttention> list = userInfoAttentionService.findAllByUserId(user.getId());
        List<UserInfoAttention> fans = userInfoAttentionService.findAllBybeUserId(user.getId());
        model.addAttribute("fans",fans.size());
        model.addAttribute("list",list);
        model.addAttribute("size",list.size());
        //共享点评数据
        List<ScenicComment> slist=null;
        slist = scenicCommentService.getFavoriteScenicByUserId(user.getId());
        //点评数
        model.addAttribute("scCount",slist.size());
        //两条点评
        if (slist.size()>2) {
            slist = slist.subList(0, 2);
        }
        model.addAttribute("slist",slist);
        //关注用户
       /* UserInfoAttention attention = new UserInfoAttention();
        attention.setBeUserId(4L);//当前用户id
        attention.setUserId(10L);//关注了当前用户的用户id
        attention.setHeadUrl("/e9299bcd-a52f-46a7-b2f9-4f0d108d5f23.jpg");//关注者的头像
        attention.setUsername("dafei");//关注者的名字
        attention.setState(0);//状态
        userInfoAttentionService.save(attention);*/

        return "mine/homepage";

    }

    @RequestMapping("/mytravelnotes")
    @RequireLogin
    public String mytravelnotes(Model model , @UserParam UserInfo userInfo){

        int todayNum = infoVisitVO.getUserInfoVisiVO(userInfo.getId()).getTodayNum();
        int visitNnm = infoVisitVO.getUserInfoVisiVO(userInfo.getId()).getVisitNnm();
        List<UserInfo> beUser = infoVisitVO.getUserInfoVisiVO(userInfo.getId()).getBeUser();
        model.addAttribute("beUser",beUser);
        model.addAttribute("todayNum",todayNum);
        model.addAttribute("visitNnm",visitNnm);
        //共享用户的信息
        UserInfo user = userInfoService.get(userInfo.getId());
        model.addAttribute("userInfo",user);

        List<Travel> list =  travelService.get2TravelByUserId(user.getId());
        //从redis中获取到当前用户所有游记的key
        int viewnum =0;
        int replynum = 0;
        for (Travel travel : list) {
            TravelStatisVO tr = travelStatisVOService.getTravelStatisVO(travel.getId());
            viewnum += tr.getViewnum();
            replynum+= tr.getReplynum();
        }
        model.addAttribute("viewnum",viewnum);
        model.addAttribute("replynum",replynum);
        model.addAttribute("list",list);
        model.addAttribute("count",list.size());

        return "mine/mytravelnotes";

    }

    @RequestMapping("/travelcollection")
    @RequireLogin
    public String travelcollection(Model model , @UserParam UserInfo userInfo){
        UserInfo user = userInfoService.get(userInfo.getId());
        model.addAttribute("user",user);
        //查询收藏的游记
        List<Long> travelIds = travelStatisVOService.getTravelCollectByUser(userInfo.getId());
        List<Travel> list = new ArrayList<>();
        if(travelIds !=null){
        for (Long travelId : travelIds) {
            Travel travel = travelService.get(travelId);
            list.add(travel);
        }}
        model.addAttribute("list",list);

        //查询收藏的攻略
        List<Long> detailIds = strategyStatisVOService.getStatisCollectByUser(user.getId());
        List<StrategyDetail> strategies = new ArrayList<>();
        for (Long detailId : detailIds) {
            StrategyDetail detail = strategyDetailService.get(detailId);
            strategies.add(detail);
        }

        model.addAttribute("strategies",strategies);
        return "mine/travelcollection";
    }
    @RequestMapping("/setting")
    public String setting(Model model , @UserParam UserInfo userInfo){
        UserInfo user = userInfoService.get(userInfo.getId());
        model.addAttribute("user",user);
        return "mine/setting";

    }

    @RequestMapping("/update")
    public String update(@UserParam UserInfo userInfo,UserInfo user) {
        user.setId(userInfo.getId());
        userInfoService.setting(user);
        return "redirect:/mine/home";
    }
    @RequestMapping("/password")
    public String password(Model model,@UserParam UserInfo userInfo) {
        UserInfo user = userInfoService.get(userInfo.getId());
        model.addAttribute("phone",user.getPhone());
        return "/mine/password";
    }


    @RequestMapping("/setPassword")
    @ResponseBody
    public Object setPassword(String phone, String nickname, String password, String rpassword, String verifyCode,@UserParam UserInfo userInfo) {
        userInfoService.setPassword(userInfo.getId(), password, rpassword, verifyCode);
        return new AjaxResult(true);
    }

    //判断验证码
    @RequestMapping("/checkCode")
    @ResponseBody
    public Object checkCode(String code,String phone){
        userInfoService.checkCode(code,phone);
        return new AjaxResult(true);
    }
    //修改手机
    @RequestMapping("/setPhone")
    @ResponseBody
    public Object setPhone(String code, String phone, @UserParam UserInfo userInfo){
        userInfoService.setPhone(code,phone,userInfo.getId());
        return new AjaxResult(true);
    }

    //根据用户id拿景点
    @RequestMapping("/review")
    public String favoriteScenic(Long uid,Model model){
        UserInfo userInfo = userInfoService.get(uid);

        List<ScenicComment> list = scenicCommentService.getFavoriteScenicByUserId(uid);
        int like=0;
        for (ScenicComment scenicComment : list) {
            int thumbupnum = scenicComment.getThumbupnum();
            like+=thumbupnum;
        }
        int size = list.size();

        model.addAttribute("userinfo",userInfo);
        model.addAttribute("like",like);
        model.addAttribute("count",size);
        model.addAttribute("list",list);
        return "mine/review";
    }
    @RequestMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response){

        Cookie[] cookies = request.getCookies();
        if(cookies != null && cookies.length > 0){
            for (Cookie cookie : cookies) {
                if("token".equals(cookie.getName())){
                    CookieUtil.removeCookie(cookie,response);
                    break;
                }
            }
        }
        //返回到登录页面
        try {
            response.sendRedirect("/");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
